package com.jt.provider.controller;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.tokens.FlowEntryToken;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class SHBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        //设置相应内容类型,以及告诉客户端按什么编码显示数
        response.setContentType("text/html;charset=utf-8");
        //设置响应状态码
        response.setStatus(429);
        //获取输出流对象
        PrintWriter out = response.getWriter();
        String msg ="访问太频繁,稍等片刻在访问!";
        if(e instanceof DegradeException) {
            msg="你再点快点看哈,滚蛋,一哈再来";
        }else if (e instanceof AuthorityException){
            msg="如需查看该内容,请出示已满18岁的身份证!!!!";
        }
        out.print(msg);
        out.flush();
        out.close();


    }
}
